//
//  AboutVC.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/10.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class AboutVC: BaseVC {

    
    @IBOutlet weak var topView: UIView!
    @IBOutlet weak var companyIV: UIImageView!
    
    @IBOutlet weak var topViewTop: NSLayoutConstraint!
    @IBOutlet weak var tableView: UITableView!
    
    var company:Company?

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        setupData()
    }
    
    private func setupUI() {
        
        topView.addShadow(type: .card)
        navBarShadowImageHidden = true
        
        tableView.register(UINib.init(nibName: "AboutTitleCell", bundle: nil), forCellReuseIdentifier: "AboutTitleCell")
        tableView.register(UINib.init(nibName: "OtherDetailContactCell", bundle: nil), forCellReuseIdentifier: "OtherDetailContactCell")
        tableView.backgroundColor = kThemeBgColor
        
        topViewTop.constant = CGFloat(kNavigationBarHeight)
    }
    
    private func setupData() {
        
        self.title = LocalizedString("about_company")
        
        if let tripid = TripManager.share.lasttripid {
            
            NetAPI.company(tripid: tripid).request(Company.self) {
                
                [weak self] response, errmsg in
                guard let `self` = self else { return }
                
                if let company = response as? Company {
                    
                    self.company = company
                    
                    self.companyIV.setImage(string: company.logo, placeholder: UIImage(named:"placeholder_squ")!)
                    self.tableView.reloadData()
                } else {
                    
                    self.view.makeToast(errmsg)
                }
            }
        }
    }
}

extension AboutVC: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if let _ = self.company {
            
            return 2
        }
        
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "AboutTitleCell") as! AboutTitleCell
            
            cell.titleLabel.text = company?.companyName
            cell.introLabel.text = company?.intro
            
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "OtherDetailContactCell") as! OtherDetailContactCell
        
        cell.contact = self.contact
        
        return cell
    }
    
    var contact:NSAttributedString? {
        
        guard let company = company else { return nil }
        
        let att = NSMutableAttributedString()
        
        if let tel = company.telephone,tel.count > 0 {
            
            let imageAtt = NSAttributedString.yy_attachmentString(withEmojiImage: UIImage(named: "电话")!, fontSize: 10)
            
            att.append(imageAtt!)
            
            let subAtt = NSMutableAttributedString(string: "  \(tel)")
            
            subAtt.yy_setTextHighlight(subAtt.yy_rangeOfAll(), color: nil, backgroundColor: nil) { _,_,_,_ in
                
                Util.phoneAction(tel: tel)
            }
            
            att.append(subAtt)
        }
        
        if let address = company.address,address.count > 0 {
            
            if att.length > 0 {
                
                att.yy_appendString("\n")
            }
            
            let imageAtt = NSAttributedString.yy_attachmentString(withEmojiImage: UIImage(named: "地址")!, fontSize: 10)
            att.append(imageAtt!)
            
            att.yy_appendString("  \(address)")
        }
        
        
//        if let email = company.email, email.count > 0 {
//
//            if att.length > 0 {
//
//                att.yy_appendString("\n")
//            }
//
//            let imageAtt = NSAttributedString.yy_attachmentString(withEmojiImage: UIImage(named: "网址")!, fontSize: 10)
//            att.append(imageAtt!)
//
//            let subAtt = NSMutableAttributedString(string: "  \(email)")
//
//            subAtt.yy_setTextHighlight(subAtt.yy_rangeOfAll(), color: nil, backgroundColor: nil) { _,_,_,_ in
//
//                Util.emailAction(email: email)
//            }
//            att.append(subAtt)
//        }
        
        if att.length == 0 {
            return nil
        }
        
        att.yy_font = kFont10
        att.yy_color = UIColor.hexStringToColor(hexString: "#222222")
        att.yy_lineSpacing = 8
        
        return att
    }
}
